<?php
/**
 * Created by b17
 * 3/23/12 8:41 PM
 */

namespace G4T\Form\Validator;

class PhoneValidator extends \Zend_Validate_Abstract
{
    private $_pattern = '/[^\d-]/';

    /**
     * Error constants
     */
    const INVALID_FORMAT = 'invalidFormat';

    /**
     * Message templates
     * @var array
     */
    protected $_messageTemplates = array(self::INVALID_FORMAT => 'Phone has wrong format. Use only digits and -.');

    /**
     * Returns true if and only if $value meets the validation requirements
     *
     * If $value fails validation, then this method returns false, and
     * getMessages() will return an array of messages that explain why the
     * validation failed.
     *
     * @param  mixed $value
     * @return boolean
     * @throws Zend_Validate_Exception If validation of $value is impossible
     */
    public function isValid($value)
    {
        if (preg_match($this->_pattern, $value)) {
            $this->_error(self::INVALID_FORMAT);
            return false;
        }
        return true;
    }
}
